package kr.co.redbean.main{
	
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quint;
	import com.greensock.easing.Sine;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.geom.Transform;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.*;
	
	import mx.events.ResizeEvent;
	
	public class SJ_Preloader extends MovieClip{
		private var CONTENT_HEIGHT:uint = 600;
		private var pb:Sprite;
		private var bg:Sprite;
		private var txt:TextField;
		private var i:Number = 1;
		private var t:Number = 0;
		
		public function SJ_Preloader(){
			stop();
			addEventListener(Event.ADDED_TO_STAGE,ADDED_TO_STAGE);
			
			// init preloader
			stage.scaleMode = "noScale";
			stage.align = StageAlign.TOP_LEFT;
			stage.frameRate = 60;
			
			var txtFormat:TextFormat;
			
			bg = new Sprite();
			pb = new Sprite();
			txt = new TextField();
			txtFormat = new TextFormat();

			pb.mouseEnabled = false;
			
			bg.mouseEnabled = false;
			bg.graphics.beginFill(0x2b1a1b);
			bg.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			bg.graphics.endFill();
			
			txtFormat.font = "Arial";
			txtFormat.size = 9;
			txtFormat.color = 0x958d8d;
			txtFormat.align = "center";
			
			txt.defaultTextFormat = txtFormat;
			txt.selectable = false;
			txt.text = "Loading: 0%";
			txt.x = int(stage.stageWidth/2 - txt.width/2);
			txt.y = int(stage.stageHeight/2 - 15);
			
			addChild(bg);
			addChild(pb);
			addChild(txt);
		}
		
		private function ADDED_TO_STAGE( $e:Event ):void {
			removeEventListener(Event.ADDED_TO_STAGE,ADDED_TO_STAGE);
			addEventListener( Event.ENTER_FRAME, hnProgress);
		}
		
		private function hnProgress( $e:Event ):void {
			graphics.clear();
			var percent:uint = 0;
			if( framesLoaded === totalFrames ){ //로딩완료시
				removeEventListener(Event.ENTER_FRAME, hnProgress);
				
				// make 100%
				txt.text = "Loading: 100%";
				pb.graphics.beginFill(0xffffff, .8);	
				pb.graphics.drawRect(0, int(stage.stageHeight/2), stage.stageWidth, 0.5);
				pb.graphics.endFill();
				
				var topBox:Sprite;
				topBox = new Sprite;
				topBox.graphics.beginFill(0x2b1a1b);
				topBox.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight/2);
				topBox.graphics.endFill();
				
				var btmBox:Sprite;
				btmBox = new Sprite;
				btmBox.graphics.beginFill(0x2b1a1b);
				btmBox.graphics.drawRect(0, stage.stageHeight/2, stage.stageWidth, stage.stageHeight/2);
				btmBox.graphics.endFill();
				
				addChildAt(topBox,1);
				addChildAt(btmBox,1);
				removeChild(bg);
				
				afterLoadingMotion(topBox, btmBox);
			}else{ //로딩중 게이지 갱신
				// TODO: tweener 적용하기
				percent = loaderInfo.bytesLoaded/loaderInfo.bytesTotal*100;
				txt.text = "Loading: " + percent + "%";
				
				pb.graphics.beginFill(0xffffff);
				pb.graphics.drawRect(int((stage.stageWidth - loaderInfo.bytesLoaded/loaderInfo.bytesTotal*stage.stageWidth)/2), int(stage.stageHeight/2)-7.5, loaderInfo.bytesLoaded/loaderInfo.bytesTotal*stage.stageWidth, 0.1);
				pb.graphics.endFill();
			}
		}
		
		private function afterLoadingMotion(topBox:Sprite, btmBox:Sprite):void {
			var tl:TimelineLite;
			tl = new TimelineLite;
			tl.appendMultiple([
				TweenLite.to(pb, .5, { alpha: 0, onComplete: hnRemove}),
				TweenLite.to(txt, .5, { alpha: 0} )]);
			tl.appendMultiple([
				TweenLite.to(topBox, .4, { y: -topBox.height, ease: Sine.easeOut, onComplete: hnResize }),
				TweenLite.to(btmBox, .4, { y: btmBox.height, ease: Sine.easeOut })]);
			tl.appendMultiple([
				TweenLite.to(topBox, .8, { y: stage.stageHeight/2 - CONTENT_HEIGHT/2, ease: Quint.easeOut, onComplete: hnLoadMain }),
				TweenLite.to(btmBox, .8, { y: stage.stageHeight/2 + CONTENT_HEIGHT/2, ease: Quint.easeOut })]);
			

			function hnRemove():void { 
				removeChild(pb);
				removeChild(txt); 
			}
	
			function hnResize():void { 
				topBox.y = 0;
				topBox.graphics.clear();
				topBox.graphics.beginFill(0x2b1a1b);
				topBox.graphics.drawRect(0, 0, stage.stageWidth, 0.5);
				topBox.graphics.endFill();
			
				btmBox.y = stage.stageHeight;
				btmBox.graphics.clear();
				btmBox.graphics.beginFill(0x2b1a1b);
				btmBox.graphics.drawRect(0, 0, stage.stageWidth, 0.5);
				btmBox.graphics.endFill();
			}
			
		}
		
		private function hnLoadMain():void {
			nextFrame();
			
			var main:Class;
			main = getDefinitionByName( 'kr.co.redbean.main.' +
				loaderInfo.loaderURL.substring(
					loaderInfo.loaderURL.lastIndexOf('/') + 1,
					loaderInfo.loaderURL.lastIndexOf('.')
				)
			) as Class;
			
			stage.addChild(new main);
			stage.removeChildAt(0);
		}
	}
}
